/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bionic.ce.businesslogic;

import com.bionic.ce.dao.IQueryDAO;
import com.bionic.ce.beans.Query;
import com.bionic.ce.connections.DAOFactory;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

/**
 *
 * @author Jarik
 */
public class QueryService {

    public static IQueryDAO queryDAO;

    static {
        DAOFactory factory = DAOFactory.getDAOFactory(DAOFactory.MYSQL);
        queryDAO = factory.getQueryDAO();
    }

    public static void createNewQuery(Query query) {
        Integer openByUser = query.getOpenByUser();
        Date openDate = query.getOpenDate();
        Date finishDate = query.getFinishDate();
        Double amount = query.getAmount();
        String currencyFrom = query.getCurrencyFrom();
        String currencyTo = query.getCurrencyTo();
        Double rateFrom = query.getRateFrom();
        Double rateTo = query.getRateTo();
        String status = query.getStatus();

        if (openByUser == null | openDate == null | finishDate == null
                | amount == null | currencyFrom.isEmpty() | currencyTo.isEmpty()
                | rateFrom == null | rateTo == null | status.isEmpty()) {
            throw new IllegalStateException("Not all required fields are filled");
        } else {
            try {
                queryDAO.add(query);
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
    }

    public static void updateCurrentQuery(Query query) {
        Integer openByUser = query.getOpenByUser();
        Date openDate = query.getOpenDate();
        Date finishDate = query.getFinishDate();
        Double amount = query.getAmount();
        String currencyFrom = query.getCurrencyFrom();
        String currencyTo = query.getCurrencyTo();
        Double rateFrom = query.getRateFrom();
        Double rateTo = query.getRateTo();
        String status = query.getStatus();

        if (openByUser == null | openDate == null | finishDate == null
                | amount == null | currencyFrom.isEmpty() | currencyTo.isEmpty()
                | rateFrom == null | rateTo == null | status.isEmpty()) {
            throw new IllegalStateException("Updates was not effected because not all required fields were filled");
        } else {
            try {
                queryDAO.update(query);
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
    }

    public static void deleteCurrentQuery(Integer queryId) {
        try {
            queryDAO.deleteById(queryId);
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }

    public static List<Query> findByDynamicSelect(Query query) {
        List<Query> list = null;
        try {
            list = queryDAO.findByDynamicSelect(query);
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return list;
    }
    
    public static List<Query> returnActiveQueries() {
        Query query = new Query();
        query.setStatus("Active");
        return findByDynamicSelect(query);
    }
    
    public static List<Query> returnActiveQueries(Integer userId) {
        Query query = new Query();
        query.setOpenByUser(userId);
        query.setStatus("Active");
        return findByDynamicSelect(query);
    }

    public static List<Query> findNotActiveQueries(Integer openByUserId) {
        List<Query> list = null;
        try {
            list = queryDAO.findNotActiveQueries(openByUserId);
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return list;
    }


    public static Query returnCurrentQueryInfo(Integer queryId) {
        Query query = new Query();
        if (queryId == null) {
            throw new IllegalStateException("Invalid Query ID");
        } else {
            query.setQueryId(queryId);
        }

        try {
            query = (Query) ((queryDAO.findByDynamicSelect(query)).toArray()[0]);
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return query;
    }
}
